package com.ten.mall.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ten.mall.service.MessageService;
import com.ten.mall.vo.MyMessage;

@Controller
public class MessageController {
	@Autowired
	private MessageService messageService;
	@RequestMapping("/admin/messageList")
	public String getAllMessage(@RequestParam(name="start",defaultValue="1") Integer start,
			   					@RequestParam(name="size",defaultValue="10") Integer size,
			   					HttpServletRequest request,String name){
		Map<String,Object> map =messageService.selectMessage(start, size, name);
		List<MyMessage> list =(List<MyMessage>) map.get("list");
		request.setAttribute("list", list);
		Long mySize=(Long) map.get("total");
		int total = mySize.intValue()/10+1;
		request.setAttribute("page", start);
		request.setAttribute("total", total);
		return "jsp/messageList";
	}
	
	@RequestMapping("/admin/toMessageAdd")
	public String toMessageAdd(Integer id,HttpServletRequest request){
		MyMessage msg =messageService.selectById(id);
		request.setAttribute("app", msg);
		return "jsp/messageAdd";
	}
	
	@RequestMapping("/admin/messageSave")
	@ResponseBody
	public String MessageSave(Integer id,MyMessage msg){
		String rs ="error";
		try {
			if(id==null){
				messageService.insertMessage(msg);
			}else {
				messageService.updateMessage(msg);
			}
			rs ="ok";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	
	@RequestMapping("/sendMsg")
	public String getMessage(HttpServletRequest request,Integer id){
		if(id!=null){
			MyMessage msg =messageService.selectById(id);
			request.setAttribute("app", msg);
		}
		return "phone/message";
	}
	
	@RequestMapping("/admin/messageDel")
	@ResponseBody
	public String messageDel(Integer id){
		String ss="ok";
		try {
			messageService.deleteMessage(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ss="error";
		}
		return ss;
	}
}
